﻿using System;
using System.ComponentModel.DataAnnotations;
using BankTransfer.Bank.Types.Enum;

namespace BankTransfer.Bank.Types.Types
{
    public class Money
    {
        private readonly decimal amount;
        private readonly Currency currency;
        public decimal Amount => amount;
        public Currency Currency => currency;

        public Money(decimal amount, Currency currency)
        {
            Check(amount);
            this.amount = amount;
            this.currency = currency;
        }

        public Money(string amount, Currency currency = Currency.RMB)
        {
            if (string.IsNullOrEmpty(amount) || !decimal.TryParse(amount, out decimal money))
            {
                throw new ValidationException("Money不正确!");
            }
            Check(money);
            this.amount = money;
            this.currency = currency;
        }

        internal int CompareTo(Money money)
        {
            return decimal.Compare(Amount, money.Amount);
        }

        private void Check(decimal value)
        {
        }

        public Money Add(Money money)
        {
            if (!Currency.Equals(money.Currency))
            {
                throw new InvalidCurrencyException();
            }
            return new Money(Amount + money.Amount, Currency);
        }
        public Money Subtract(Money money)
        {
            if (!Currency.Equals(money.Currency))
            {
                throw new InvalidCurrencyException();
            }
            return new Money(Amount - money.Amount, Currency);
        }
    }
}